﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class ProjectTasks : PageBaseClass
{
    MyDMSEntity _myDmsEntity=new MyDMSEntity();
    int projectCode;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["pcode"] != null && Request["pcode"] != "")
        {
            projectCode = int.Parse(Request["pcode"]);
        }

        if (!IsPostBack)
        {
            BindProjects();
            BindTasks();
        }
    }

    private void BindProjects()
    {
        //object dataObject;
        //new SqlFactory().ExecuteQuery("select p.* from Project p " +
        //    " inner join UserInProject uip on p.Project_Code = uip.Project_Code " +
        //    " where uip.User_Code = " + _author.user_Code, out dataObject, SqlFactory.DataObjectType.DataSet);

            ddlMyProjects.DataSource = (from p in _myDmsEntity.Project
                                        join uip in _myDmsEntity.UserInProject on p.Project_Code equals uip.Project_Code
                                        where uip.User_Code == Author.user_Code
                                        select p).ToList();
            ddlMyProjects.DataTextField = "Project_Title";
            ddlMyProjects.DataValueField = "Project_Code";
            ddlMyProjects.DataBind();

            ddlMyProjects.Items.Insert(0, new ListItem("All", "0"));

            if (projectCode > 0)
            {
                ddlMyProjects.Text = projectCode.ToString();
            }
    }

    private void BindTasks()
    {
        List<Task> lstProjTask = new List<Task>();

        if (projectCode > 0)
        {
            var pts = _myDmsEntity.Task.Where(pt => pt.Project_Code == projectCode);
            foreach (Task pt in pts)
            {
                if (pt.Assigned_To == Author.user_Code)
                {
                    lstProjTask.Add(pt);
                }
            }
        }
        else
        {
            lstProjTask = _myDmsEntity.Task.Where(pt => pt.Assigned_To == Author.user_Code).ToList();
        }

        gvProjectTasks.DataSource = lstProjTask;
        gvProjectTasks.DataBind();
    }

    protected void ddlMyProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        projectCode = int.Parse(ddlMyProjects.SelectedValue);
        BindTasks();
    }

    protected void btnCloseTask_Click(object sender, EventArgs e)
    {
        //TODO:Implement this
        //if (new SqlFactory().ExecuteNonQuery("update ProjectTask set Closed_Date = " + DateTime.Now.ToString() +
        //     ", Closed_By = " + _author.user_Code))
        //{
        //    lblStatus.Text = "Closed Successfully ";
        //}
        //else
        //{
        //    lblStatus.Text = "Not closed";
        //}
    }
}
